<template>
	<view class="payment-pages">
		<loading v-if="showLoading"></loading>
		<CustomNav :isBack="true" color="#fff" :topBgColor="topBgColor" title="燃气缴费"></CustomNav>
		<view class="top-block">
			<view class="info-block">
				<view class="title-item">
					<view class="title FZHZGBJ">余额</view>
				</view>
				<view class="price-item">￥
					<text class="price FZHZGBJ">{{info.balance}}</text>
					<text style="color: #767676;">元</text>
				</view>
				<view class="info-item">
					<view class="name">户号</view>
					<view class="text">{{info.account}}</view>
				</view>
				<view class="info-item">
					<view class="name">用户名</view>
					<view class="text">{{info.userName}}</view>
				</view>
				<view class="info-item">
					<view class="name">缴费单位</view>
					<view class="text">{{info.companyName}}</view>
				</view>
				<view class="info-item">
					<view class="name">用户地址</view>
					<view class="text">{{info.userAddress}}</view>
				</view>
			</view>
		</view>
		<view class="payment-block">
			<view class="title FZHZGBJ">缴费金额</view>
			<view class="payment-info">
				<view class="payment-list" v-for="(item,index) in list" :key="item.id"
					:class="tabActive==item.id ? 'active' :''" @click="changeActive(item.id,index)">
					{{item.quota}}元
				</view>
			</view>
			<view class="form-item">
				<view class="unit">￥</view>
				<input placeholder="点击输入缴费金额" v-model="price" class="form-crl" type="number" maxlength="4"
					@focus="changeInput" />
				<view class="iconfont icon-gengduo"></view>
			</view>
			<view class="btn-item">
				<view class="btn normal" @click="payment">立即缴费</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getPaymentListApi,
		getGasDetailsInfoApi,
		createPayGasOrder
	} from '@/api/payment.js'
	export default {
		data() {
			return {
				info: {
					balance: 0,
					account: '',
					userName: '',
					companyName: '',
					userAddress: ''
				},
				list: [],
				tabActive: 0,
				price: '',
				infoList: [],
				detail: {
					account: '',
					companyName: ''
				},
				payMoney: 0,
				changePrice: 0
			}
		},
		methods: {
			changeActive(e, index) {
				this.tabActive = e
				this.changePrice = this.list[index].quota
				this.price = ''
			},
			changeInput() {
				this.tabActive = 0;
				this.changePrice = 0;
			},
			getPayList() {
				return getPaymentListApi().then((res) => {
					this.list = res
				});
			},
			getDetail() {
				return getGasDetailsInfoApi({
					account: this.detail.account,
					companyName: this.detail.companyName
				}).then((res) => {
					if (res.gasDetails) {
						this.info = res.gasDetails
					} else {
						this.showToast('请输入正确的户号', () => {
							uni.navigateBack()
						}, 1000)
					}
				});
			},
			payment() {
				if (this.tabActive !== 0 || this.price !== '') {
					let price = parseFloat(this.price) || 0
					this.payMoney = this.changePrice + price
					let that = this;
					createPayGasOrder({
						account: this.detail.account,
						companyName: this.detail.companyName,
						payMoney: this.payMoney
					}).then((res) => {
						let appId = res.appId;
						let timeStamp = res.timeStamp;
						let nonceStr = res.nonceStr;
						let signType = res.signType;
						let paySign = res.paySign;
						let prepayId = res.prepayId;
						let prePayParams = res.prePayParams;
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: timeStamp,
							nonceStr: nonceStr,
							package: res.package,
							signType: 'MD5',
							paySign: paySign,
							success(res) {
								// uni.showToast({
								// 	title: '缴费成功',
								// 	duration: 2000,
								// 	mask: true,
								// })
								that.showToast('缴费成功', () => {
									uni.switchTab({
										url: '/pages/index/index'
									})
								}, 1000)
							}
						});
					});
				} else {
					this.showToast('请选择或输入缴费金额！');
				}
			}
		},
		onLoad(e) {
			// uni.$on('queryDetail', data => {
			// 	this.detail = data;
			// })
			this.detail.account = e.account;
			this.detail.companyName = e.companyName;
			uni.showCustomLoading()
			Promise.all([this.getPayList(), this.getDetail()]).finally(() => {
				uni.hideCustomLoading()
			})
		},
		// onUnload() {
		// 	uni.$off('queryDetail')
		// }
	}
</script>

<style lang="scss" scoped>
	.payment-pages {
		overflow: hidden;
		min-height: 100vh;
		background-color: #F4F8FF;

		.top-block {
			padding: 180rpx 30rpx 20rpx 30rpx;
			background-image: linear-gradient(to bottom, #3153B6, #4173E7);

			.info-block {
				background: #FFFFFF;
				padding: 30rpx;
				border-radius: 20rpx;

				.title-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
				}

				.title {
					margin-bottom: 30rpx;
					font-size: 32rpx;
				}

				.price-item {
					display: flex;
					align-items: center;
					color: #4376E4;
					font-size: 28rpx;
					margin-bottom: 20rpx;

					.price {
						font-size: 44rpx;
						margin-right: 10rpx;
					}
				}

				.info-item {
					display: flex;
					font-size: 30rpx;
					line-height: 65rpx;

					.name {
						width: 160rpx;
						color: #767676;
					}

					.text {
						width: calc(100% - 160rpx);
					}
				}
			}
		}

		.payment-block {
			width: calc(100% - 60rpx);
			margin: 20rpx auto;
			background-color: #FFFFFF;
			padding: 30rpx;
			border-radius: 20rpx;

			.payment-info {
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				margin-top: 30rpx;

				.payment-list:nth-child(3n) {
					margin-right: 0;
				}

				.payment-list.active {
					background-color: #4071E5;
					color: #FFFFFF;
				}

				.payment-list {
					width: calc((100% - 40rpx) / 3);
					margin-right: 20rpx;
					margin-bottom: 20rpx;
					background: #F6F6F6;
					padding: 24rpx 30rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-weight: bold;
					border-radius: 15rpx;
					font-size: 30rpx;
				}
			}

			.form-item {
				display: flex;
				align-items: center;
				justify-content: space-around;
				border-bottom: 1px solid #F7F7F7;
				line-height: 90rpx;
				font-size: 30rpx;

				.unit {
					font-weight: bold;
					margin-right: 10rpx;
				}

				.form-crl {
					width: calc(100% - 100rpx);
				}

				.iconfont {
					color: #767676;
					font-size: 34rpx;
				}
			}

			.btn-item {
				display: flex;
				justify-content: center;
				margin-top: 40rpx;
			}
		}
	}
</style>